/*
 * AuthorPanelSeeReviews.java
 * Author: Tyler Simrell
 * 
 * Changed the layout of the panel to look more slick and conform better
 * to the rest of the ConferenceGuru.
 * Edited by: Lawrence Grass
 */
package gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.AbstractAction;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import model.Author;
import model.Conference;
import model.ConferencePaper;
import model.Review;


@SuppressWarnings("serial")
public class AuthorPanelSeeReviews extends JPanel
{
	private static final Dimension Button_SIZE = new Dimension(150, 30);
	private static final Dimension TITLE_SIZE = new Dimension(170, 30);
	Author my_role;
	Conference my_conference;
	ArrayList<Review> currentPaperReviews;
	JButton saveButton;
	JFileChooser fileChooser;
	JPanel reviewPanel;
	
	//This constructor puts the passed in variables into the class level variables, then 
	//calls setUpPanel();
	public AuthorPanelSeeReviews(Author the_author, Conference the_conference)
	{
		super();
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		setBackground(BaseViewFrame.BACKGROUND_COLOR);
		setBorder(BorderFactory.createEmptyBorder());
		my_role = the_conference.getAuthorRole(the_author.getName());
		my_conference = the_conference;
		setUpPanel();
	}
	
	
	//This method does all the major heavy lifting of creating the GUI to be displayed 
	// on the workspace view.
	//Edited/co-authored: Lawrence Grass
	private void setUpPanel()
	{
		removeAll();
		fileChooser = new JFileChooser();
		
		ArrayList<ConferencePaper> papers = new ArrayList<ConferencePaper>();
		for(ConferencePaper p : my_conference.getProgramChair().getPapers())
		{
			
			if(my_role.getName().equals(p.getAuthorName()))
			{
				papers.add(p);
			}
		}
		
		//sets up the list of papers with there reviews, if any.
		for(ConferencePaper p: papers)
		{
			final ConferencePaper copy = p;
			JPanel paper_panels = new JPanel();
			paper_panels.setLayout(new BoxLayout(paper_panels, BoxLayout.X_AXIS));
			paper_panels.setMaximumSize(new Dimension(1200, 40));
			paper_panels.setBackground(BaseViewFrame.BACKGROUND_COLOR);
			paper_panels.setBorder(BorderFactory.createEtchedBorder());
			
			//Shows the paper title
			JLabel paper_title = new JLabel(p.getTitle());
			paper_title.setMaximumSize(TITLE_SIZE);
			paper_panels.add(paper_title);
			
			//Creates the place holder buttons grayed out for reviews
			ArrayList<Component> review_stuffs = new ArrayList<Component>();
			for(int i = 0; i <=3; i++)
			{
				JButton place_holder = new JButton("No reviews yet");
				place_holder.setEnabled(false);
				place_holder.setMaximumSize(Button_SIZE);
				review_stuffs.add(place_holder);
			}
			
			//replaces the place holder buttons with real ones if there is a review submitted.
			for(final Review r : p.getReviews())
			{
				int i = 0;
				if (r.isReviewed())
				{					
					JButton review = new JButton("Review by: " + r.getReviewer());
					review.setMaximumSize(new Dimension(175, 30));
					review_stuffs.set(i, review);
					review.addActionListener(new AbstractAction()
				      {  
						@Override
				        public void actionPerformed(final ActionEvent the_event)
				        {
							int userChoice = fileChooser.showSaveDialog(null);
							if(userChoice == JFileChooser.APPROVE_OPTION)
							{
								copyFileSave(fileChooser.getSelectedFile(), r);
							}
				        }
				      });
					i++;
					
				}			
			}
		
			//Adds the buttons to the papers panel.
			for(Component c : review_stuffs)
			{
				paper_panels.add(c);
				paper_panels.add(Box.createHorizontalStrut(7));

			}
			
			//Create the resubmit button for editing.
			JButton resubmitButton = new JButton("Resubmit Manuscript");
			resubmitButton.addActionListener(new AbstractAction()
				      {  
						@Override
				        public void actionPerformed(final ActionEvent the_event)
				        {
							int userChoice = fileChooser.showSaveDialog(null);
							if(userChoice == JFileChooser.APPROVE_OPTION)
							{
								File conferenceFilePath = null;
								File sentInFile = fileChooser.getSelectedFile();
								if(sentInFile.exists())
								{
									conferenceFilePath = copyFile(sentInFile);
									copy.setManuscriptFile(conferenceFilePath.getAbsolutePath());
								}
							}
				        }
				      });
			paper_panels.add(resubmitButton);
			
			//Create the delete button to delete a paper.
			JButton deleteButton = new JButton("Delete Manuscript");
			deleteButton.addActionListener(new AbstractAction()
		      {  
				@Override
		        public void actionPerformed(final ActionEvent the_event)
		        {
					int choice = JOptionPane.showConfirmDialog(null, "Are You Sure" +
							" you want to delete your manuscript?", "Are You Sure?", JOptionPane.YES_NO_OPTION);
					if(choice == JOptionPane.YES_OPTION)
					{
						ArrayList<ConferencePaper> papers = (ArrayList<ConferencePaper>) 
								my_conference.getConferencePapers();
						papers.remove(copy);
						revalidate();
						setUpPanel();
						repaint();
					}
		        }
		      });
			paper_panels.add(deleteButton);
			add(paper_panels);
			add(Box.createVerticalStrut(10));
		}	
	}
	
	//This method copies the file into the file directory by using the FileInputStream
	//and the FileOutputStream <Java API is quite helpful here>. It is a modification
	//of the copyFile method from the AuthorPanelSubmit. It actually works in reverse
	//and deals with reviews and not manuscripts.
	private File copyFileSave(File the_user_copy, Review the_review)
	{
		File newFile = null;
		FileInputStream input;
		try 
		{
			input = new FileInputStream(the_review.getReview());
			FileOutputStream output = new FileOutputStream(the_user_copy);
			byte[] stream = new byte[input.available()];
			input.read(stream);
			output.write(stream);
			output.close();
			input.close();
		}
		catch (FileNotFoundException e) 
		{
			System.err.println("File not found. Whoops!");
		} 
		
		catch (IOException e) 
		{
			System.err.println("IO Exception");
		}
		
		return newFile;
	}
	
	//This method copies the file into the file directory by using the FileInputStream
	//and the FileOutputStream <Java API is quite helpful here>. 
	private File copyFile(File the_input_file)
	{
		File newFile = null;
		FileInputStream input;
		try 
		{
			input = new FileInputStream(the_input_file);
			newFile = new File("Manuscripts/" , the_input_file.getName());
			FileOutputStream output = new FileOutputStream(newFile);
			byte[] stream = new byte[input.available()];
			input.read(stream);
			output.write(stream);
			output.close();
			input.close();
		}
		catch (FileNotFoundException e) 
		{
			System.err.println("File not found. Whoops!");
		} 
		
		catch (IOException e) 
		{
			System.err.println("IO Exception");
		}
		
		return newFile;
	}
}
